/**
 * 
 */
package com.automation.demo.testNG;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

/**
 * @author David
 *
 */
public abstract class PSLBase {
	
	private static ThreadLocal<WebDriver> thread = new ThreadLocal<WebDriver>();
	
	private String baseUrl;
	
	@BeforeMethod
	@Parameters({"seleniumHub", "browser", "webSite"})
	public void startSession(String seleniumHub, String browser, String webSite) throws Exception {
		WebDriver driver = new RemoteWebDriver(new URL(seleniumHub), getBrowser(browser)); 
	    baseUrl = webSite;
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		thread.set(driver);
	}
	
	/**
	 * 
	 * @param browser
	 * @return
	 */
	private DesiredCapabilities getBrowser(String browser) {
		 if ("chrome".equalsIgnoreCase(browser)) {
			return  DesiredCapabilities.chrome();
		 } else {
			 return DesiredCapabilities.firefox();
		 }
	}
 
	@AfterMethod
	public void closeSession() throws Exception {
		getDriver().quit();
	}
	
	/**
	 * 
	 * @return
	 */
	protected WebDriver getDriver() {
		return thread.get();
	}
	
	/**
	 * 
	 * @return
	 */
	protected String getBaseUrl() {
		return this.baseUrl;
	}

}
